// 分类
<template>
  <div class='top-category'>
    <div class="container">
      <!-- 顶级类目 -->
      <!-- 面包屑导航 -->

      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{name}}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider :sliders="sliders"/>
      <!-- 所有二级分类 -->
<div class="sub-list">
    <h3>全部分类</h3>
    <ul>
      <li v-for="i in list" :key="i.id">
        <a href="javascript:;">
          <img :src="i.picture" >
          <p>{{i.name}}</p>
        </a>
      </li>
    </ul>
</div>
<!-- 分类关联商品 -->
<div class="ref-goods" v-for="item in list" :key="item.id">
    <div class="head">
      <h3>{{item.name}}</h3>
      <XtxMore to="/" />
    </div>
    <div class="body">
<Goodsitem v-for="good in item.goods" :good="good" :key="good.id"/>
 </div>
</div>
    </div>
  </div>
</template>

<script>
import { findBanner } from '@/api/home'
import { onMounted, ref } from 'vue'
import { findTopCategory } from '@/api/category'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import Goodsitem from './components/goods-item.vue'
export default {
  name: 'TopCategory',
  components: {
    Goodsitem
  },
  setup () {
    const sliders = ref([])// 轮播图数据
    async function rc () {
      const res = await findBanner()
      console.log('轮', res)
      sliders.value = res.result
    }
    const route = useRoute()// 即this.$route就可以拿到数据了
    const name = ref('')
    const list = ref([])// 全部分类数据
    async function re (id) {
      const re = await findTopCategory(id)
      console.log(re)
      list.value = re.result.children
      name.value = re.result.name
    }
    onMounted(() => {
      rc()
      re(route.params.id)
    })
    onBeforeRouteUpdate((to) => {
      // to指代的是目标路由对象，to拿到最新的路由参数id
      // 使用最新id获取当前点击过的数据
      re(to.params.id)
    })
    return {
      sliders,
      list,
      name
    }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
.goods-item {
    display: block;
    width: 220px;
    padding: 20px 30px;
    text-align: center;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
</style>
